package com.keyancompany.zhongbaopingtai.adapter;

import android.content.Context;
import android.text.Html;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.keyancompany.zhongbaopingtai.R;
import com.keyancompany.zhongbaopingtai.api.UserAPI;
import com.keyancompany.zhongbaopingtai.listener.IClickCancleListener;
import com.keyancompany.zhongbaopingtai.model.FindModel;

import java.util.List;

/**
 * Created by firefox on 2016/6/14.
 */
public class FindAdapter extends ListAdapter<FindModel> {

    private IClickCancleListener listener;


    public FindAdapter(Context ctx, IClickCancleListener listener) {
        super(ctx);
        this.listener = listener;
    }

    @Override
    public int getContentViewId() {
        return R.layout.item_find;
    }

    @Override
    public HolderView getHoldView(int position, View contentView, ViewGroup parent) {
        FindHolder hold = new FindHolder();
        hold.parent = (LinearLayout) contentView.findViewById(R.id.parent);
        hold.collectionIcon = (ImageView) contentView.findViewById(R.id.find_collection);
        hold.technology = (TextView) contentView.findViewById(R.id.jinengyaoqiu);
        hold.time = (TextView) contentView.findViewById(R.id.find_time);
        hold.author = (TextView) contentView.findViewById(R.id.find_author);
        hold.content = (TextView) contentView.findViewById(R.id.find_content);
        hold.title = (TextView) contentView.findViewById(R.id.find_title);
        hold.yusuan = (TextView) contentView.findViewById(R.id.find_yusuan);
        hold.zhouqi = (TextView) contentView.findViewById(R.id.find_zhouqi);
        return hold;
    }

    @Override
    public void setItemView(final int position, final HolderView contentView, final FindModel item) {
        FindHolder hold = (FindHolder) contentView;
        hold.collectionIcon.setVisibility(item.getIsMe() == 1 ? View.GONE : View.VISIBLE);
        hold.collectionIcon.setImageResource(item.getCollectFlag() == 1 ? R.mipmap.hascollection : R.mipmap.collection);
        hold.title.setText(item.getTitle());
        hold.content.setText(Html.fromHtml(item.getDescription()));
        hold.time.setText(item.getCreateTime());
        hold.author.setText(item.getPhone());
        hold.yusuan.setText(ctx.getString(R.string.yusuan_text, "" + item.getBudget()));
        hold.zhouqi.setText(ctx.getString(R.string.zhouqi_text, item.getEndTime()));
        hold.technology.setText(item.getState());
        hold.collectionIcon.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (listener != null)
                    listener.clickCancle(position);
            }
        });

        hold.parent.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (itemClickListener != null) {
                    itemClickListener.listItemClick(item, position);
                }
            }
        });
    }

    IOnItemClickListener itemClickListener;

    public void setItemClickListener(IOnItemClickListener itemClickListener) {
        this.itemClickListener = itemClickListener;
    }

    public interface IOnItemClickListener {
        void listItemClick(FindModel item, int position);
    }


    class FindHolder extends HolderView {

        ImageView collectionIcon;

        TextView title;

        TextView content;

        TextView time;

        TextView author;

        TextView technology;

        TextView yusuan;

        TextView zhouqi;

        LinearLayout parent;
    }
}
